package com.hnd.springframework.beans.factory.support;

import com.hnd.springframework.beans.BeansException;
import com.hnd.springframework.beans.factory.config.BeanDefinition;


import java.util.*;

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
    Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();

    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new BeansException("no bean define["+beanName+"]");
        }
        return beanDefinition;
    }

    @Override
    public boolean containBeansDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }

    @Override
    public String [] getBeanNames() {
        List<String> keys = new ArrayList();
        beanDefinitionMap.entrySet().stream().forEach(x -> keys.add(x.getKey()));
        return keys.toArray(new String[0]);
    }

    @Override
    public void registryBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    @Override
    public void registryBeanDefinition(BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanDefinition.getBeanName(), beanDefinition);
    }

}
